/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.oai.metadataRepository.metadata;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.Interval;
import org.joda.time.LocalDateTime;
import org.joda.time.chrono.ISOChronology;

/**
 *
 * @author Martin Rehanek, Martin.Rehanek@gmail.com
 */
public class DateStamp implements Comparable {

    public static DateStamp MIN = new DateStamp("1970-01-01T00:00:00Z");
    public static DateStamp MAX = new DateStamp("9999-01-01T00:00:00Z");
    private LocalDateTime ldt;

    public DateStamp() {
        ldt = new LocalDateTime(ISOChronology.getInstance(DateTimeZone.UTC));
    }

    public DateStamp(LocalDateTime ldt) {
        this.ldt = ldt;
    }

    public DateStamp(String year, String month, String day) {
        int yearInt = Integer.valueOf(year).intValue();
        int monthInt = Integer.valueOf(month).intValue();
        int dayInt = Integer.valueOf(day).intValue();
        ldt = new LocalDateTime(yearInt, monthInt, dayInt, 0, 0);
    }

    public DateStamp(String dateStamp) {
        String[] tokens = dateStamp.split("T");
        String[] yearMonthDay = tokens[0].split("-");
        String[] hourMinuteSecondZ = tokens[1].split(":");
        int year = Integer.parseInt(yearMonthDay[0]);
        int month = Integer.parseInt(yearMonthDay[1]);
        int day = Integer.parseInt(yearMonthDay[2]);
        int hour = Integer.parseInt(hourMinuteSecondZ[0]);
        int minute = Integer.parseInt(hourMinuteSecondZ[1]);
        int second = Integer.parseInt(hourMinuteSecondZ[2].substring(0, hourMinuteSecondZ[2].length() - 1));
        ldt = new LocalDateTime(year, month, day, hour, minute, second, 0);
    }

    @Override
    public String toString() {
        return getLdt().toString().split("\\.")[0] + 'Z';
    }

    /**
     *
     * @param object
     * @return 1 if 'this' is older than 'object', 0 if they are about the same and -1 if 'this' is younger than 'object'
     */
    public int compareTo(Object object) {
        if (object == null) {
            throw new NullPointerException();
        }
        if (object instanceof DateStamp) {
            DateStamp ds = (DateStamp) object;
            Interval interval;
            DateTime first = this.ldt.toDateTime();
            DateTime second = ds.ldt.toDateTime();
//            boolean debug = ds.ldt.getMonthOfYear() == 6 && ds.ldt.getDayOfMonth() == 3;

            if (first.isBefore(second)) {
                interval = new Interval(first, second);
//                if (debug) {
//                    System.out.println("duration: " + interval.toDurationMillis());
//                }
                return interval.toDurationMillis() < 1000 ? 0 : -1;
            } else {
                interval = new Interval(second, first);
                //System.out.println("duration: " + interval.toDurationMillis());
                return interval.toDurationMillis() < 1000 ? 0 : 1;
            }
        } else {
            throw new IllegalArgumentException("object must be an instance of DateStamp");
        }
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null || !(obj instanceof DateStamp)) {
            return false;
        }
        return ldt.equals(((DateStamp) obj).getLdt());
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 37 * hash + (this.ldt != null ? this.ldt.hashCode() : 0);
        return hash;
    }

    /**
     * @return the ldt
     */
    public LocalDateTime getLdt() {
        return ldt;
    }

    public static DateStamp dateStampOfYesterday() {
        DateStamp now = new DateStamp();
        DateStamp yesterday = new DateStamp(now.getLdt().minusDays(1));
        return yesterday;//.toString();
    }

    public static DateStamp dateStampWeekAgo() {
        DateStamp now = new DateStamp();
        DateStamp yesterday = new DateStamp(now.getLdt().minusWeeks(1));
        return yesterday;//.toString();
    }
}
